package com.example.siyun.service.impl.shouyeserviceimpl;

import com.example.siyun.mapper.shouyemapper.DhMapper;
import com.example.siyun.pojo.Dhl;
import com.example.siyun.service.shouyeservice.DhService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
/**
 * @Auther: 全
 * @Date: 2022/9/26 09:33
 * @Description:导航栏
 */
@Service
public class DhServiceImpl implements DhService {
    @Autowired
    private DhMapper dhMapper;
    /**
     * 获取导航栏的信息
     * @param pid:导航栏的父id,username 获取登录的用户名
     * @return 导航栏的数据
     */
    @Transactional
    @Override
    public List<Dhl> getDh(Integer pid,String username) {
        ArrayList<Dhl> objects = new ArrayList<>();
        List<Dhl> ej = dhMapper.getEj(pid,username);
        for (Dhl e:ej
             ) {
            Dhl dhl = new Dhl();
            dhl.setMenuId(e.getMenuId());
            dhl.setName(e.getName());
            dhl.setUrl(e.getUrl());
            dhl.setSpread(e.getSpread());
            List<Dhl> dh = getDh(e.getMenuId(),username);
            dhl.setChildMenus(dh);
            objects.add(dhl);
        }
        return objects;
    }

    @Override
    public List<Dhl> getalldhl(String name) {
        //获取一级菜单
        List<Dhl> parent = dhMapper.getEj(0,name);
        //获取全部菜单
        List<Dhl> childall=dhMapper.getalldhl(name);
        List<Dhl> list = aa(parent, childall);
        return list;
    }
    public List<Dhl> aa(List<Dhl> parent,List<Dhl> child){
        List<Dhl> list = new ArrayList<>();
        for(Dhl d1:parent){
            for(Dhl all1:child) {
                if(d1.getMenuId()==(all1.getPid())){
                    list.add(all1);
                }
            }
            d1.setChildMenus(list);
            list = new ArrayList<>();
            aa(d1.getChildMenus(),child);
        }
        return parent;
    }
}
